package com.hotchpotch.activity;

import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import com.hotchpotch.R;
import com.hotchpotch.bean.City;
import com.hotchpotch.bean.ProvinceBean;
import com.hotchpotch.util.JsonUtil;
import com.hotchpotch.util.Utils;
import com.hotchpotch.bean.AddressDtailsEntity;
import com.hotchpotch.bean.AddressModel;
import com.hotchpotch.bean.CountryBean;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/8 0008.
 */

public class OneActivity extends AppCompatActivity {
    private final String TAG = getLocalClassName();
    private TextView textView1, textView2, textView3, textView4;
    private List<AddressDtailsEntity> addrModels = new ArrayList<>();

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    textView1.setText("原始数据大小:" + addrModels.size());
                    break;
                case 1:
                    textView2.setText("省级数据大小" + proList.size());
                    break;
                case 2:
                    textView3.setText("市级数据大小" + cityList.size());
                    break;
                case 3:
                    textView4.setText("区县级数据大小" + areaList.size());
                    break;
                case 4:
                    countryBean.provinceList.addAll(proList);
                    String str = JsonUtil.toJson(countryBean);
//                    writeTxtToFile(str, filePath, fileName);
                    saveFile(str);
                    Toast.makeText(OneActivity.this, "写入完成!", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    public static void saveFile(String str) {
        String filePath = null;
        boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (hasSDCard) { // SD卡根目录的hello.text
            filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "helloAddress.txt";
        } else  // 系统下载缓存根目录的hello.text
            filePath = Environment.getDownloadCacheDirectory().toString() + File.separator + "helloAddress.txt";

        try {
            File file = new File(filePath);
            if (!file.exists()) {
                File dir = new File(file.getParent());
                dir.mkdirs();
                file.createNewFile();
            }
            FileOutputStream outStream = new FileOutputStream(file);
            outStream.write(str.getBytes());
            outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        initView();
    }

    private void initView() {
        textView1 = (TextView) findViewById(R.id.textview);
        textView2 = (TextView) findViewById(R.id.textview1);
        textView3 = (TextView) findViewById(R.id.textview2);
        textView4 = (TextView) findViewById(R.id.textview3);
        initData();
    }

    private void initData() {
        String address = Utils.readAssert(this, "region.txt");
        AddressModel model = JsonUtil.parseJson(address, AddressModel.class);
        if (model != null) {
            addrModels.addAll(model.RECORDS);
            handler.sendEmptyMessage(0);
        }
        getSize();
        getCityList();
        getProList();
        handler.sendEmptyMessageDelayed(4, 5000);
    }

    /**
     * 转化
     */
    private CountryBean countryBean = new CountryBean();
    private List<ProvinceBean> proList = new ArrayList<>();
    private List<City> cityList = new ArrayList<>();
    private List<AddressDtailsEntity> areaList = new ArrayList<>();

    private void getSize() {
        countryBean.region_id = 1;
        countryBean.parent_id = 0;
        countryBean.region_name = "中国";
        countryBean.region_type = 0;
        countryBean.agency_id = 0;
        for (int i = 0; i < addrModels.size(); i++) {//省级集合
            if (addrModels.get(i).region_type == 1) {
                ProvinceBean pro = new ProvinceBean();
                pro.region_id = addrModels.get(i).region_id;
                pro.parent_id = addrModels.get(i).parent_id;
                pro.region_name = addrModels.get(i).region_name;
                pro.region_type = addrModels.get(i).region_type;
                pro.agency_id = addrModels.get(i).agency_id;
                proList.add(pro);
            }
        }
        handler.sendEmptyMessageAtTime(1, 3000);

        for (int i = 0; i < addrModels.size(); i++) {//市级集合
            if (addrModels.get(i).region_type == 2) {
                City city = new City();
                city.region_id = addrModels.get(i).region_id;
                city.parent_id = addrModels.get(i).parent_id;
                city.region_name = addrModels.get(i).region_name;
                city.region_type = addrModels.get(i).region_type;
                city.agency_id = addrModels.get(i).agency_id;
                cityList.add(city);
            }
        }
        handler.sendEmptyMessageDelayed(2, 3000);

        for (int i = 0; i < addrModels.size(); i++) {//区县级集合
            if (addrModels.get(i).region_type == 3) {
                AddressDtailsEntity areaBean = new AddressDtailsEntity();
                areaBean.region_id = addrModels.get(i).region_id;
                areaBean.parent_id = addrModels.get(i).parent_id;
                areaBean.region_name = addrModels.get(i).region_name;
                areaBean.region_type = addrModels.get(i).region_type;
                areaBean.agency_id = addrModels.get(i).agency_id;
                areaList.add(areaBean);
            }
        }
        handler.sendEmptyMessageDelayed(3, 3000);
    }

    private void getCityList() {
        int cityId = 0;
        int cityWhich = 0;
        for (int i = 0; i < cityList.size(); i++) {
            cityId = cityList.get(i).region_id;
//            Log.e(TAG, "市级循环的id = " + cityId);
            cityWhich = i;
            for (int j = 0; j < areaList.size(); j++) {
                if (areaList.get(j).parent_id == cityId) {
//                    Log.e(TAG, "区县级的循环父id相等时 = " + areaList.get(j).parent_id);
                    cityList.get(cityWhich).areaList.add(areaList.get(j));
                }
            }
        }
    }

    private void getProList() {
        int proId = 0;
        for (int i = 0; i < proList.size(); i++) {
            proId = proList.get(i).region_id;
            for (int j = 0; j < cityList.size(); j++) {
                if (cityList.get(j).parent_id == proId) {
                    proList.get(i).cityList.add(cityList.get(j));
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
